﻿function Paging(config) {
    this.config = config;
    this.total = this.config.total;
    this.cur = this.config.cur;
    this.around = this.config.around;
    this.result = [];
    this.baseCount = this.around * 2 + 1 + 2 + 2 + 2;
    this.superlus = this.baseCount - 4;
    this.startEllipsis = 1 + this.around + 2 + 1;
    this.endEllipsis = this.total - 2 - this.around - 1;
    this.init();
    this.prev();
}
Paging.prototype = {
    construct: Paging,
    init: function (total, cur, around) {
        let self = this;
        if (self.total <= self.baseCount - 2) {
            for (var i = 0; i < self.total; i++) {
                self.result.push(i + 1);
            }
        } else {
            if (self.cur < self.startEllipsis) {
                for (var i = 0; i < self.superlus; i++) {
                    self.result.push(i + 1);
                }
                self.result.push("...", self.total);
            } else if (self.cur > self.endEllipsis) {
                for (var i = 0; i < self.superlus; i++) {
                    self.result.push(i + self.endEllipsis - 1);
                }
                self.result.unshift(1,"...");
            } else {
                for (var i = 0; i < self.around * 2 + 1; i++) {
                    self.result.push(i + self.cur - self.around);
                }
                self.result.unshift(1,"...");
                self.result.push("...", self.total);
            }
        }
        self.Bind();
    },
    Bind: function () {
        var self = this;
        this.box = document.createElement("ul");
        this.box.className = "box";
        this.inner = document.createElement("li");
        this.inner.className = "box-prev";
        this.inner.innerHTML = "上一页";
        this.outer = document.createElement("li");
        this.outer.innerHTML = "下一页";
        this.outer.className = "box-next";
        this.box.appendChild(this.inner);
        for (let i = 0; i < self.result.length; i++) {
            let _li = document.createElement("li");
            _li.className = "box-inner";
            _li.innerHTML = self.result[i];
            _li.onclick = function () {
                if(this.innerHTML!=="..."){
                    self.cur = self.result[i];
                    document.body.removeChild(self.box);
                    new Paging({total: self.total, cur: self.cur, around: self.around});
                }
            };
            self.result[i] === self.cur?_li.style.color = "#6C9ECC":_li.style.color = "#000";
            this.box.appendChild(_li);
        }
        this.box.appendChild(this.outer);
        document.body.appendChild(this.box);
    },
    prev: function () {
        var self = this;
        self.inner.onclick = function () {
            if (self.cur > 1) {
                self.cur = self.cur - 1;
            } else if (self.cur === 1) {
                alert("已经是第一页了");
                self.cur = 1;
            }
            document.body.removeChild(self.box);
            new Paging({total: self.total, cur: self.cur, around: self.around});
        };
        self.outer.onclick = function () {
            if (self.cur > 1 && self.cur !== self.total) {
                self.cur = self.cur + 1;
            } else if (self.cur === self.total) {
                alert("已经没有了");
                self.cur = self.total;
            }
            document.body.removeChild(self.box);
            new Paging({total: self.total, cur: self.cur, around: self.around});
        };
    }
};
new Paging({total: 1000, cur: 50, around: 2});
